import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TabsPage } from "../tabs/tabs";
import { SignupPage } from "../signup/signup";
import { GetVerificationCodePage } from "../get-verification-code/get-verification-code";
import { HttpServiceProvider } from '../../providers/http-service/http-service';
import { UsrInfoProvider } from '../../providers/usr-info/usr-info';
import { UserInfoData } from '../../model/UserInfoData';
import { UserIdToken, UserOptions } from '../../interfaces/user-options';
import { NgForm, FormBuilder, Validators, FormGroup } from '@angular/forms';
/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  /**
   *
   */
  listData:any;
  login_data: UserOptions = { account: '', password: '' };
  userIdToken: UserIdToken ={id: '',token: ''};
  loginForm: FormGroup;
  submitted = false;
  data: any;

  constructor(public navCtrl: NavController, public navParams: NavParams,private httpServiceProvider: HttpServiceProvider,
    private usrInfoProvider : UsrInfoProvider,
    // private userInfoData : UserInfoData
    private formBuilder: FormBuilder
  ) {

    this.loginForm = formBuilder.group({

      username: ['', Validators.compose([Validators.minLength(11), Validators.maxLength(11), Validators.required, Validators.pattern("^(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$")])],
      password: ['', Validators.compose([Validators.required, Validators.minLength(6)])]

    })
  }
  /**
   * 用户登录
   */
  onLogin() {

    this.httpServiceProvider.login(this.login_data).then((result) => {

      this.data = result;
      if(this.data.code==1)
      {
        this.userIdToken=this.data.data;
        this.usrInfoProvider.write("usr_info",this.userIdToken);
        this.navCtrl.setRoot(TabsPage);
      }
    }, (err) => {

      console.log(err);

    });

  }

  onSignup() {
    this.navCtrl.push(SignupPage);
  }

  /**
   *
   *
   * @memberof LoginPage
   */
  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

/**
 * 
 */
  GoToSignup() {
    this.navCtrl.push(SignupPage);
  }
  /**
   * 
   * @param username 
   * @param password 
   */
  GoToGetPWD(username: HTMLInputElement, password: HTMLInputElement) {
    this.navCtrl.push(GetVerificationCodePage);
  }
}
